package com.jcfk.system.service;

import cn.hutool.core.util.StrUtil;
import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.jcfk.common.business.service.BaseService;
import com.jcfk.common.core.utils.BeanUtils;
import com.jcfk.common.security.utils.SecurityUtils;
import com.jcfk.common.web.domain.RetResult;
import com.jcfk.system.dao.RefUserRoleMappingDao;
import com.jcfk.system.dao.SysDictionaryItemDao;
import com.jcfk.system.domain.dto.RefUserRoleMappingDTO;
import com.jcfk.system.domain.dto.SysDictionaryItemDTO;
import com.jcfk.system.domain.dto.SysUserDTO;
import com.jcfk.system.domain.po.RefUserRoleMapping;
import com.jcfk.system.domain.po.SysDictionaryItem;
import com.jcfk.system.domain.query.RefUserRoleMappingQuery;
import com.jcfk.system.domain.query.SysDictionaryItemQuery;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collector;
import java.util.stream.Collectors;

/**
 * 用户角色关联 服务
 */
@Slf4j
@Service
public class RefUserRoleMappingService extends BaseService<RefUserRoleMappingDao, RefUserRoleMapping> {
    /**
     * 根据条件获取单表信息列表
     *
     * @param query 查询条件
     * @return List<SampleSimpleTableDTO>
     */
    public List<RefUserRoleMappingDTO> getList(RefUserRoleMappingQuery query) {
        // 查询条件
        QueryWrapper<RefUserRoleMapping> qw = new QueryWrapper<>();
        if (!StrUtil.isEmpty(query.getUserId())) {
            qw.lambda().eq(RefUserRoleMapping::getUserId, query.getUserId());
        }
        if (!StrUtil.isEmpty(query.getRoleId())) {
            qw.lambda().eq(RefUserRoleMapping::getRoleId, query.getRoleId());
        }
        // 查询数据
        List<RefUserRoleMapping> list = super.list(qw);
        List<RefUserRoleMappingDTO> dtoList = new ArrayList<>();
        for (RefUserRoleMapping po : list) {
            RefUserRoleMappingDTO dto = new RefUserRoleMappingDTO();
            BeanUtils.copyProperties(po, dto);

            dtoList.add(dto);
        }
        return dtoList;
    }

    /**
     * 保存关联
     *
     * @param userId
     * @param ids
     */
    public void saveList(String userId, List<String> ids) {

        // 删除原有关联
        QueryWrapper<RefUserRoleMapping> qw = new QueryWrapper<>();
        if (StrUtil.isNotEmpty(userId)) {
            qw.lambda().eq(RefUserRoleMapping::getUserId, userId);
        }

        this.baseMapper.delete(qw);

        List<RefUserRoleMapping> saveList = new ArrayList<>();
        for (String id : ids) {
            RefUserRoleMapping mapping = new RefUserRoleMapping();
            mapping.setUserId(userId);
            mapping.setRoleId(id);
            mapping.setCreateTime(new Date());
            mapping.setCreatedUserId(SecurityUtils.getUserId());
            saveList.add(mapping);
        }
        // 保存关联信息
        if (saveList.size() > 0) this.saveBatch(saveList);
    }

    /**
     * 根据用户删除关联
     *
     * @param userId
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteListByUser(String userId) {
        // 删除条件
        QueryWrapper<RefUserRoleMapping> qw = new QueryWrapper<>();
        qw.lambda().eq(RefUserRoleMapping::getUserId, userId);
        return this.remove(qw);
    }

    /**
     * 根据用户删除关联
     *
     * @param userIds
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteListByUser(List<String> userIds) {
        Boolean result = true;
        // 删除条件
        QueryWrapper<RefUserRoleMapping> qw = new QueryWrapper<>();
        qw.lambda().in(RefUserRoleMapping::getUserId, userIds);
        result = this.remove(qw);
        return result;
    }

    /**
     * 根据角色删除关联
     *
     * @param roleId
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteListByRole(String roleId) {
        // 删除条件
        QueryWrapper<RefUserRoleMapping> qw = new QueryWrapper<>();
        qw.lambda().eq(RefUserRoleMapping::getRoleId, roleId);
        return this.remove(qw);
    }

    /**
     * 根据角色删除关联
     *
     * @param roleIds
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteListByRole(List<String> roleIds) {
        // 删除条件
        QueryWrapper<RefUserRoleMapping> qw = new QueryWrapper<>();
        qw.lambda().in(RefUserRoleMapping::getRoleId, roleIds);
        return this.remove(qw);
    }

    /**
     * 设置添加默认值
     *
     * @param dto 添加对象
     */
    public void setDefaultValue(RefUserRoleMappingDTO dto) {
        if (StrUtil.isEmpty(dto.getCreatedUserId())) {
            dto.setCreatedUserId(SecurityUtils.getUserId());
            dto.setCreateTime(new Date());
        }
        dto.setLastUpdatedUserId(SecurityUtils.getUserId());
        dto.setLastUpdateTime(new Date());
    }

}
